package com.gm.baseplatform.core.utils;

import java.util.regex.Pattern;

public class CheckUtil {
	
	public static final String NUMBER_PATTERN = "^[0-9]*$";
	
	public static final String NUMBER_GT_ZERO = "^\\d*[1-9]\\d*$";
	
	public static final String NUMBER_LT_ZERO = "^-[1-9]+$";
	
	public static final String NUMBER_LETTER_PATTERN="^[0-9a-zA-Z]*$";
	
	public static final String NUMBER_LETTER_CHINESE_PATTERN="^[0-9a-zA-Z\\u4E00-\\u9FA5]*$";
	
	public static String CHINESE_PATTERN="^[\\u4E00-\\u9FA5]+$";
	
	public static String TEL_MOBILE_PATTERN="^(([0]\\d{2,3}-)?\\d{7,8})|(1[3458][0-9]{9})$";
	
	public static String ZIPCODE_PATTERN="^\\d{6}$";
	
	public static final String IDENTITYCARD_PATTERN = "\\d{15}|\\d{17}[\\dx]";
	
	public static final String MOBILENUM_PATTERN = "(134|135|136|137|138|139|150|151|152|157|158|159|182|183|187|188|147)\\d{8}";

	public static final String EMAIL_PATTERN = "[\\w-]+(\\.[\\w-]+)*@[\\w-]+(\\.[\\w-]+)+";
	
	public static String ascPattern = "^(0(?=1|$)|1(?=2|$)|2(?=3|$)|3(?=4|$)|4(?=5|$)|5(?=6|$)|6(?=7|$)|7(?=8|$)|8(?=9|$)|9(?=0|$)|0(?=$))*$";
	
	public static String descPattern = "^(9(?=8|$)|8(?=7|$)|7(?=6|$)|6(?=5|$)|5(?=4|$)|4(?=3|$)|3(?=2|$)|2(?=1|$)|1(?=0|$)|0(?=$))*$";
	
	public static String samePattern = "^(9(?=9|$)|8(?=8|$)|7(?=7|$)|6(?=6|$)|5(?=5|$)|4(?=4|$)|3(?=3|$)|2(?=2|$)|1(?=1|$)|0(?=0|$))*$";

	public static String moneyPattern = "([0-9][\\d]*(\\.[\\d]{1,2})?)|(0\\.([0][1-9]|[1-9][\\d]|[0-9]))";
	
	public static String PHONE_PATTERN = "\\d{11}";
	
	public static String WEB_XSS_FILTER_RULE = "\\[,\\],\\{,\\},\\<,\\>,\\*,\\|,\\',\\+,\\(,\\),CR,LF,DECLARE,CAST,EXEC,and";
	

	public static boolean isLowPassword(String password){
		boolean isPassword = false;
		if(Pattern.matches(ascPattern, password) || Pattern.matches(descPattern, password) || Pattern.matches(samePattern, password)){
			isPassword = true;
		}
		return isPassword;
	}
	
	public static boolean isIdentityNo(String identityNo){
		boolean isNo = false;
		if(Pattern.matches(IDENTITYCARD_PATTERN, identityNo)){
			isNo = true;
		}
		return isNo;
	}
	
	public static boolean isEmail(String inputEmail){
		boolean isChecked = false;
		if(Pattern.matches(EMAIL_PATTERN, inputEmail)){
			isChecked = true;
		}
		return isChecked;
	}
	
	public static boolean isNumber(String inputStr){
		boolean isChecked = false;
		if(Pattern.matches(NUMBER_PATTERN, inputStr)){
			isChecked = true;
		}
		return isChecked;
	}
	
	public static boolean isNumberLetter(String inputStr){
		boolean isChecked = false;
		if(Pattern.matches(NUMBER_LETTER_PATTERN, inputStr)){
			isChecked = true;
		}
		return isChecked;
	}
	
	public static boolean isNumberLetterChinese(String inputStr){
		boolean isChecked = false;
		if(Pattern.matches(NUMBER_LETTER_CHINESE_PATTERN, inputStr)){
			isChecked = true;
		}
		return isChecked;
	}
	
	public static boolean isChinese(String inputStr){
		boolean isChecked = false;
		if(Pattern.matches(CHINESE_PATTERN, inputStr)){
			isChecked = true;
		}
		return isChecked;
	}
	
	public static boolean isTelMobile(String inputStr){
		boolean isChecked = false;
		if(Pattern.matches(TEL_MOBILE_PATTERN, inputStr)){
			isChecked = true;
		}
		return isChecked;
	}
	
	public static boolean isZipCode(String inputStr){
		boolean isChecked = false;
		if(Pattern.matches(ZIPCODE_PATTERN, inputStr)){
			isChecked = true;
		}
		return isChecked;
	}
	
	public static boolean isMobileNo(String inputNo){
		boolean isChecked = false;
		if(Pattern.matches(MOBILENUM_PATTERN, inputNo)){
			isChecked = true;
		}
		return isChecked;
	}

	public static boolean isBlack(String val){
		if(val == null){
			return false;
		}
		if("".equals(val.trim())){
			return false;
		}
		return true;
	}
	
	public static boolean isMoney(String inputMoney){
		boolean isChecked = false;
		if(Pattern.matches(moneyPattern, inputMoney)){
			isChecked = true;
		}
		return isChecked;
	}
	
	public static boolean isGtZero(String number){
		boolean isChecked = false;
		if(Pattern.matches(NUMBER_GT_ZERO, number)){
			isChecked = true;
		}
		return isChecked;
	}
	
	public static boolean isLtZero(String number){
		boolean isChecked = false;
		if(Pattern.matches(NUMBER_LT_ZERO, number)){
			isChecked = true;
		}
		return isChecked;
	}
	
	public static boolean isPhoneNo(String inputNo){
		boolean isChecked = false;
		if(Pattern.matches(PHONE_PATTERN, inputNo)){
			isChecked = true;
		}
		return isChecked;
	}
	
	public static String webXssFilter(String source) {
	   
		if (source == null || source.equals("")) {
			return source;
		}
		
		String[] filterRules = WEB_XSS_FILTER_RULE.split(",");
		if (filterRules == null || filterRules.length <= 0) {
		   return source;
		}
		
		for (int i = 0, j = filterRules.length; i < j; i++) {
			 source = source.replaceAll(filterRules[i],"");
		}
		
		return source;
	}

}
